#ifdef __LOG_HPP__

#define __LOG_HPP__

#endif

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <filesystem>
#include "Mutex.hpp"
#include "Cond.hpp"

namespace LogModule
{
    using namespace MutexModule;
    class LogStrategy
    {
    public:
        ~LogStrategy() = default;
        virtual void SyncLog(const std::string &message) = 0; // 纯虚函数
    };

    // print in consoles
    class ConsoleLogStrategy : public LogStrategy
    {
    public:
        ConsoleLogStrategy() {}
        void SyncLog(const std::string &message) override
        {
            {
                LockGuard lock(_mutex);
                std::cout << message << std::endl;
            }
        }

        ~ConsoleLogStrategy() {}

    private:
        Mutex _mutex;
    };

    // print in files
    std::string defaultpath = "./log";
    std::string defaultfile = "my.log";
    class FileLogStrategy : public LogStrategy
    {
    public:
        FileLogStrategy(const std::string &path = defaultpath, const std::string &file = defaultfile)
            : _path(path), _file(file)
        {
            {
                LockGuard lock(_mutex);
                if (std::filesystem::exists(_path))
                {
                    return;
                }

                // if not exist, then create it
                try
                {
                    std::filesystem::create_directories(_path);
                }
                catch (const std::filesystem::filesystem_error &e)
                {
                    std::cerr << e.what() << '\n';
                }
            }
        }

        void SyncLog(const std::string &message)
        {
            {
                LockGuard lock(_mutex);
                std::cout << message << std::endl;
            }
        }

        ~FileLogStrategy()
        {
        }

    private:
        Mutex _mutex;
        std::string _path;
        std::string _file;
    };

    // Logger 类: 管理日志的策略
    class Logger
    {
    public:
        Logger()
        {
            // default mode is console
            EnableConsoleLogStrategy();
        }

        void EnableFileLogStrategy() // 使用文件策略
        {
            _fflush_strategy = std::make_unique<FileLogStrategy>();
        }

        void EnableConsoleLogStrategy() // 使用显示器策略
        {
            _fflush_strategy = std::make_unique<ConsoleLogStrategy>();
        }

        // 用类内类来定义一个日志类，因为类内类可以访问外部类的私有成员
        // 使用时，Logger对象调用operator()，返回一个LogMessage临时对象，用完即销毁
        class LogMessage
        {
        public:
            // 传入参数？？？？？？？
            LogMessage(){};
        private:
        };

    private:
        std::unique_ptr<LogStrategy> _fflush_strategy;
    };
}